/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package savet.gui.dialogos;

import savet.gui.modelos.ListaLegendaModel;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

/**
 * Render para JList com JCheckbox
 *
 * @author Adisson Marinho
 */
public class CheckListRenderer extends JCheckBox
        implements ListCellRenderer {

    public CheckListRenderer() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
//                JList list = (JList) event.getSource();
//
//                // Get index of item clicked
//                int index = list.locationToIndex(event.getPoint());
//                ListaLegendaModel model = (ListaLegendaModel) list.getModel();
//                model.setSelecaoLegenda(index);

                // Repaint cell
                System.out.println("Mouse clicked...");
//                list.repaint(list.getCellBounds(index, index));
            }
        });
    }

    public Component getListCellRendererComponent(
            JList list, Object value, int index,
            boolean isSelected, boolean hasFocus) {
        ListaLegendaModel model = (ListaLegendaModel) list.getModel();

        setEnabled(list.isEnabled());
        setSelected(model.isLegendaSelecionada(index));
        setFont(list.getFont());
        if (isSelected) {
            setBackground(list.getSelectionBackground());
            setForeground(list.getSelectionForeground());
        } else {
            setBackground(list.getBackground());
            setForeground(list.getForeground());
        }
        setText(value.toString());
        return this;
    }
}
